<%+header%>
<!--
    This module gives some convinience to access the shell via web, in
    case that you don't have a uart console or telnet/ssh connection.
    Hua Shao <nossiac@163.com>
-->
<h2><a name="content">Web Console</a></h2>
<form id="console">
    <fieldset class="cbi-section">
        <legend>Execute shell commands or scripts as root. <span style="color:red">Be Careful</span>.</legend>
        <p>Press <b>Enter</b> to execute. Press <b>Shift+Enter</b> to start a new line.</p>
        <p><textarea name="cmd" id="cmd" style="width:98%;height:4em;"></textarea></p>
        <p>
            <button class="cbi-button" onclick="return postcmd('ifconfig -a')">ifconfig -a</button>
            <button class="cbi-button" onclick="return postcmd('cat /proc/meminfo')">meminfo</button>
            <button class="cbi-button" onclick="return postcmd('uci show')">uci</button>
            <button class="cbi-button" onclick="return postcmd('ps w')">ps</button>
            <button class="cbi-button" onclick="return postcmd('cat /proc/mtd')">mtd</button>
            <button class="cbi-button" onclick="return postcmd('block info')">block info</button>
            <button class="cbi-button" onclick="return postcmd('mount')">mount</button>
        <pre id="result" style="background-color:black;color:white;height:auto;min-height:200px;width:98%;"></pre>
    </fieldset>
</form>

<script type="text/javascript">

function postcmd(cmd) {
    (new XHR()).post("<%=luci.dispatcher.build_url("admin", "mtk", "webcmd")%>", {"cmd":cmd}, function(x) {
        console.log(x.response)
        console.log(x)
        document.getElementById("result").innerHTML = x.response;
    });
    return false;
}

document.getElementById("cmd").addEventListener("keydown", function(e) {
    if (!e) { var e = window.event; }

    if (e.keyCode == 13 && !e.shiftKey) {
        e.preventDefault();
        var cmd = document.getElementById("cmd");
        postcmd(cmd.value);
        cmd.value = "";
        return true;
    }
}, false);

</script>

<%+footer%>

